/*******************************************************************************
Project:		Wealth -- Smith, Zidar, and Zwick
Last modified: 	2021-06-15
Description:	This files builds inputs for a version of the interest rate CMD 
				exercise in which group 1 is the top 1% and group 2 is the 
				bottom 99%. It pulls data from SZ parameters, FRED, the SCF, and 
				collapses of tax data.
*******************************************************************************/

*Environment Variables

cd "~\cmd_replication_public\matlab"

*Dropbox folder
global dboxdirwlth " "
global litroot "$dboxdirwlth/lit"
global dboxstatadirwlth "$dboxdirwlth/stata"
global dtadir "$dboxstatadirwlth/dta"
global rawdir "$dboxstatadirwlth/raw"

global preferred_defn_early_v2 = 88
global preferred_defn_mid_v2 = 98
global preferred_defn_late_v2 = 112

global niw_defn_early = $preferred_defn_early_v2 + 100
global niw_defn_mid = $preferred_defn_mid_v2 + 100
global niw_defn_late = $preferred_defn_late_v2 + 100
/*******************************************************************************
Helper programs which can also be found in programs_final.do
*******************************************************************************/
capture program drop es_rank_scf/*%<*/
program define es_rank_scf
	syntax, rankvar(string) [othersplitvars(string) outname(string)]

	/***************************************************************************
		(1.0) Parse program inputs
	***************************************************************************/

	foreach varname in `rankvar' `othersplitvars' {
		capture confirm numeric variable `varname', exact
		if _rc > 0 {
			di as error "Variable `varname' not found; do not abbreviate variable names"
			exit 111
		}
	}

	foreach addtlvar in married year {
		capture confirm variable `addtlvar'
		if _rc > 0 {
			di as error "Need `addtlvar' variable"
			exit 111
		}
	}

	if regexm("`othersplitvars'", "`rankvar'") {
		di as error "Warning: ranking variable substring contained within othersplitvars"
		di as error "Ranking variable may be split twice"
	}

	/***************************************************************************
		(1.1) Make SCF rows into ``individuals:'' duplicate rows representing 
			married couples
	***************************************************************************/

	expand 2 if married == 1

	foreach splitvar of varlist `rankvar' `othersplitvars' {
		replace `splitvar' = `splitvar' / 2 if married == 1
	}

	cumul `rankvar' [aw = wgt], by(year) gen(rank)

	tempfile scf_indvls
	save `scf_indvls'

	/***************************************************************************
		(1.2) Load in counts of equal split individuals from SZ 2020 parameters 
			file
	***************************************************************************/

	import excel using $litroot/.../parameters.xlsx, firstrow clear

	keep if inrange(yr, 1989, 2019) & mod(yr - 1989, 3) == 0

	keep yr totadults20 
	rename yr year

	assert totadults20 < 1E6
	replace totadults20 = totadults20 * 1E3 

	sort year

	tempfile parameters
	save `parameters'

	/***************************************************************************
		(1.3) Merge parameters counts onto SCF data and adjust ranking variable
	***************************************************************************/

	use `scf_indvls', clear

	merge m:1 year using `parameters', assert(2 3) keep(3) nogen

    sort year
    egen num_ind = total(wgt), by(year) // Get total individuals according to SCF 

    /* For each observation, compute the percent of individuals in  
        the US (as represented by SCF) with wealth rank above that 
        observation */
    gen pct_ind_above = 1 - rank

    /* Now use this to compute number of households with wealth rank  
        above that observation */
    gen num_ind_above = pct_ind_above * num_ind

    /* Divide the number of HHs in US with greater rank by total  
        number of tax units in that year minus 400 TUs. */
    gen pct_ind_above_taxdata = num_ind_above / (totadults20 - 400)

    /* The rank of the observation among all tax units (excluding the 
        Forbes 400) is one minus the percent of non-Forbes 400 tax units 
        holding greater wealth than the observation */
    if "`outname'" == "" {
    	di "No output name specified; naming adjusted rank es_rank"
    	gen es_rank = 1 - pct_ind_above_taxdata
    }
    else {
    	gen `outname' = 1 - pct_ind_above_taxdata	
    }
    drop num_ind pct_ind_above num_ind_above pct_ind_above_taxdata totadults20 rank

end/*%>*/

capture program drop load_taxdata/*%<*/
program define load_taxdata
	syntax, rankspec(int) [startyr(string) endyr(string) year(string) units(string) top0001(string)]

	/***************************************************************************
		(0) Parse program inputs
	***************************************************************************/

	if "`year'" != "" & real("`year'") != . {
		local startyr = `year'
		local endyr = `year'
	}

	if "`startyr'" == "" {
		di "No startyr specified; assuming start year = 1966"
		local startyr = 1966
	}
	if "`endyr'" == "" {
		di "No endyr specified; assuming end year = 2016"
		local endyr = 2016
	}

	if "`units'" != "" & !inlist("`units'", "tu", "es") {
		di as error "Invalid split level; must be es or tu"
		exit
	}
	else if "`units'" == "" {
		di "No units specified; assuming individuals (equal split)"
		local units = "es"
	}

	/***************************************************************************
		(1) Load in data from start year to end year, constructing baseline 
			variables.
	***************************************************************************/

	forv year = `startyr' / `endyr' {

		/***********************************************************************
			(1.1) Read in files
		***********************************************************************/

        if (`rankspec' < 3000) {

            if ("`top0001'" == "") {

                import delim using $.../wealth_scen`rankspec'_`units'_`year'.csv, clear
            }
            else if ("`top0001'" != "") {
                import delim using $.../wealth_scen`rankspec'_`units'_`year'_top0001.csv, clear
            }
        } 
        else {
            import delim using $.../wealth_scen`rankspec'_`units'_`year'.csv, clear
        }

	
		/***********************************************************************
			(1.2) Construct preferred wealth portfolio allocations
		***********************************************************************/

        /* From wealthbuild-20220114.do
        spec 3016:
                taxbond_muf taxbond_info muni currency  
                ownerhome_szz rentalhome ownermort rentalmort nonmort_ini
                ccorw_9010  
                penw_szz_scaled_v8
                scorw_szzhybnof_scaled partw_szzhybnof_sz20_scaled solepropw_szz_sz20_scaled
                miscw
        spec 3115:
                taxbond_muf taxbond_cmd_3tier muni currency  
                ownerhome_szz rentalhome ownermort rentalmort nonmort_ini
                ccorw_9010  
                penw_szz_scaled_v8
                scorw_szz_scaled partw_szz_sz20_scaled solepropw_szz_sz20_scaled
                miscw
        spec 3215:
                taxbond_muf taxbond_cmd_3tier muni currency  
                ownerhome_ini rentalhome ownermort_ini rentalmort nonmort_ini
                ccorw_9010  
                szz_penw_pre1980_v8
                scorw_szz_scaled partw_szz_sz20_scaled solepropw_szz_sz20_scaled
                miscw
        spec 3017: (our preferred spec without DB)
                taxbond_muf taxbond_info muni currency  
                ownerhome_szz rentalhome ownermort rentalmort nonmort
                ccorw_9010  
                penw_szz_scaled_v8
                s_value_avg_ebitda_hybnof_red p_value_avg_ebitda_hybnof_red solepropw
                missing_scorp missing_pship 
                miscw
        spec 3007: 
                taxbond taxbond_mufmisc_SZ muni currency
                ownerhome rentalhome ownermort rentalmort nonmort_ini
                ccorw  
                hwpen_ini
                scorw partw_sz20_scaled solepropw_sz20_scaled
        spec 3018: 
                taxbond_equal muni currency
                ownerhome rentalhome ownermort rentalmort nonmort_ini
                ccorw_equal  
                hwpen_ini
                scorw partw_sz20_scaled solepropw_sz20_scaled
                miscw
        */
		
        if inlist(`rankspec', 20) {
            if `year' > 1979 {
                gen penw_szz_scaled_v8 = .
            }
            else {
                gen szz_penw_pre1980_v8 = .
				xx
            }
        }

		if `year' > 2000 {
            gen pthru_pref = solepropw + s_value_avg_ebitda_hybnof_red + ///
                                 p_value_avg_ebitda_hybnof_red + missing_pship + missing_scorp
            gen pthru_equ = scorw + partw_sz20_scaled + solepropw_sz20_scaled
            gen pthru_base = solepropw_szz_sz20_scaled + scorw_szzhybnof_scaled + partw_szzhybnof_sz20_scaled
			gen hwfix_pref = taxbond_muf + taxbond_info + muni + currency
            gen hwfix_equ = taxbond + muni + currency + taxbond_mufmisc_sz
            gen hwfix_base = hwfix_pref
            gen hwfix_equal = taxbond_equal + muni + currency 
		}
		else {
			gen pthru_pref = scorw + partw + solepropw + missing_pship + missing_scorp
            gen pthru_equ = scorw + partw_sz20_scaled + solepropw_sz20_scaled
            gen pthru_base = solepropw_szz_sz20_scaled + scorw_szz_scaled + partw_szz_sz20_scaled  
			gen hwfix_pref = taxbond_cmd_3tier + muni + currency + taxbond_muf
            gen hwfix_equ = taxbond + muni + currency + taxbond_mufmisc_sz
            gen hwfix_base = hwfix_pref
            gen hwfix_equal = taxbond_equal + muni + currency 
		}

		if `year' > 1979 {
			gen hwhou_pref = ownerhome_szz + rentalhome + ownermort + rentalmort
			gen hwhou_equ = ownerhome + rentalhome + ownermort + rentalmort
			gen hwhou_base = hwhou_pref
            * TODO: fix by adding this var to the collapses
			capture gen hwpen_pref = penw_szz_scaled_db_v8
			capture gen hwpen_pref = penw_szz_scaled 
            gen hwpen_equ = hwpen_ini
			*gen hwpen_base = penw_szz_scaled_v8
		}
		else {
			gen hwhou_pref = ownerhome_ini + rentalhome + ownermort_ini + rentalmort
			gen hwhou_equ = ownerhome_ini + rentalhome + ownermort_ini + rentalmort
			gen hwhou_base = hwhou_pref
			gen hwpen_pref = szz_penw_pre1980
            gen hwpen_equ = hwpen_ini
            *gen hwpen_base = szz_penw_pre1980_v8
		}

		gen hwbus_pref = (0.2 * ccorw_9010) + pthru_pref
		gen hwbus_base = (0.2 * ccorw_9010) + pthru_base
        gen hwbus_equ = (0.2 * ccorw) + pthru_equ
		gen hwequ_pref = 0.8 * ccorw_9010 
        gen hwequ_base = hwequ_pref
        gen hwequ_equ = 0.8 * ccorw

        gen ccorw_pref = ccorw_9010 
        gen ccorw_base = ccorw_pref
        gen ccorw_equ = ccorw

        * Don't have miscw for some of the new specs because they're folded into
        * taxbond per SZ16/PSZ18
        if inlist(`rankspec', 3007, 3107, 3207, 3018, 3118, 3218) {
            gen miscw_hweal`rankspec' = 0
        }

		if inlist(`rankspec', 188, 198, 212) {
			local niwspec = `rankspec' - 100
			gen miscw = miscw_hweal`niwspec'
            gen hwoth_pref = nonmort + miscw
            gen hwoth_base = nonmort_ini + miscw
		}
        else if inlist(`rankspec', 3516, 3615, 3715) {
			local niwspec = `rankspec' - 500
			gen miscw = miscw_hweal`niwspec'
            gen hwoth_pref = nonmort + miscw
            gen hwoth_base = nonmort_ini + miscw
        }
		else if inlist(`rankspec', 88, 98, 112) | `rankspec' > 3000 { // Only have miscw for updated parameters
			gen miscw = miscw_hweal`rankspec'
            gen hwoth_pref = nonmort + miscw
            gen hwoth_base = nonmort_ini + miscw
            gen hwoth_equ = nonmort_ini
		}
		else if inlist(`rankspec', 118) {
			gen miscw = miscw_hweal`rankspec'
            gen hwoth_pref = nonmort + miscw
            gen hwoth_base = nonmort_ini + miscw
		}

        * Forbes categories
        if (`year' > 1981 & !inlist(`rankspec', 20, 198, 212) & `rankspec' < 3000) {
            replace hwfix_pref = hwfix_pref + forbes400_hwfix
            replace hwequ_pref = hwequ_pref + forbes400_hwequ
            replace hwbus_pref = hwbus_pref + forbes400_hwbus
            replace hwhou_pref = hwhou_pref + forbes400_hwhou
            replace hwoth_pref = hwoth_pref + forbes400_hwoth

            replace pthru_pref = pthru_pref + forbes400_pthru 
            replace ccorw_pref = ccorw_pref + forbes400_ccorw
        } 

		/***********************************************************************
			(1.3) Construct baseline income variables
		***********************************************************************/

        if ("`top0001'" == "") {

            gen inc_fix = fiint + intest
            gen inc_scorp = scorpinc2
            gen inc_div = fidiv + divest
            gen inc_kg = fikgi + kgest
            gen inc_bus = (schcincp+rylinc+partpinc2)
            gen inc_fiscal = fninc 
            gen inc_pen = peninc
            gen inc_ini = ptinc

        }

		/***********************************************************************
			(1.4) Generate year variable
		***********************************************************************/

		gen year = `year'

		/***********************************************************************
			(1.5) Create mutually exclusive groups
		***********************************************************************/

        if ("`top0001'" == "") {
            count
            if (`r(N)' == 6) {
                intermediate_levels6 `rankspec'
            }
            else if (`r(N)' == 5) {
                intermediate_levels5 `rankspec'
            }
        }

		tempfile tax`year'
		save `tax`year''

	}

	clear

	/***************************************************************************
		(2) Append data files in memory
	***************************************************************************/

	forv year = `startyr'/`endyr' {
		append using `tax`year''
	}

	order group year
	ds group year, not
	order `r(varlist)', alphabetic last

	compress

	di "Data in memory"

end/*%>*/

capture program drop intermediate_levels6/*%<*/
program define intermediate_levels6
	args ranking
	
	drop if regexm(group, "aires")
	count
	assert `r(N)' == 6

	assert group[1] == "All"
	assert group[2] == "P90-100"
	assert group[3] == "P99-100"
	assert group[4] == "P99.9-100"
	assert group[5] == "P99.99-100" 
	assert group[6] == "P99.999-100" 

	set obs `=_N+5'
	capture confirm variable w`ranking'_group
	if _rc != 0 {
		gen w`ranking'_group = _n
	}
	else {
        * TODO: For now recoding the P99.999 to work with DS code. Can make this
        * less delicate.
        replace w`ranking'_group = 6 if w`ranking'_group == 9
		replace w`ranking'_group = _n if missing(w`ranking'_group)
	}

	replace group = "P0-90" in `=_N-4'	
	replace group = "P90-99" in `=_N-3'
	replace group = "P99-99.9" in `=_N-2'
	replace group = "P99.9-99.99" in `=_N-1'
	replace group = "P99.99-99.999" in `=_N'
	
	sort w`ranking'_group
	capture confirm variable threshold 
	if _rc == 0 {
		ds *group year threshold, not
	}
	else {
		ds *group year, not
	}
	foreach var of varlist `r(varlist)' {
		replace `var'  = `var'[1] - `var'[2] in `=_N-4'
		replace `var'  = `var'[2] - `var'[3] in `=_N-3'
		replace `var'  = `var'[3] - `var'[4] in `=_N-2'
		replace `var'  = `var'[4] - `var'[5] in `=_N-1'
		replace `var'  = `var'[5] - `var'[6] in `=_N'
	}
	sort w`ranking'_group
	replace year = year[1] if mi(year)

end/*%>*/

/*******************************************************************************
	(0) Prepare interest rates and CPI adjustments data from FRED
*******************************************************************************/

freduse AAA BAA DGS10 DGS5 CPIAUCSL, clear

gen year = yofd(daten)

collapse (mean) CPIAUCSL r_aaa = AAA r_baa = BAA r_ust10 = DGS10 r_ust5 = DGS5, by(year)

qui keep if inrange(year, 1966, 2019)

foreach rate of varlist r_* {
	gen ln_`rate' = ln(`rate')
}

qui sort year
assert year[_N] == 2019

qui gen cpi_adjfactor = CPIAUCSL[_N] / CPIAUCSL
drop CPIAUCSL

tempfile fred
save `fred'

/*******************************************************************************
	(1) Prepare counts of equal-split individuals by year
*******************************************************************************/

import excel using $litroot/.../parameters.xlsx, ///
    firstrow clear

keep yr ttinttaxw
qui keep if inrange(yr, 1966, 2019)

rename yr year

merge 1:1 year using `fred', keepusing(cpi_adjfactor) assert(3) nogen

qui replace ttinttaxw = ttinttaxw * cpi_adjfactor

* Fixed claims assets are in millions; scale into trillions
qui gen a_total_sz20 = ttinttaxw / 1E6
qui gen ln_a_total_sz20 = ln(a_total_sz20)

isid year
sort year

tempfile parameters
save `parameters'

/*******************************************************************************
	(2) Prepare SCF data
*******************************************************************************/

foreach rankvar in niw intinc agi nonintagi { // Cycle through ranking variables

	/***************************************************************************
		(2.1) Load data and drop redundant variables; rename for simplicity
	***************************************************************************/

	use $dtadir/scf_revision.dta, clear // Load microfile
	
	*changed to stmutf from obmutf
	replace fixmutf = (0.5 * comutf) + gbmutf + stmutf + mmmf + (0.5 * omutf)

	* Keep only necessary variables
	keep year wgt networth_pref intinc agi fixmutf inttaxw married

	qui gen niw = networth_pref - inttaxw - fixmutf // Create non-interest wealth
	qui gen nonintagi = agi - intinc

	/***************************************************************************
		(2.2) Implement equal-split ranking
	***************************************************************************/

	ds year wgt `rankvar' married, not
	es_rank_scf, rankvar(`rankvar') othersplitvars("`r(varlist)'")

	qui gen `rankvar'_grp = cond(es_rank > 0.99, 1, 2) // Group into top 0.1/bot99.9

	/***************************************************************************
		(2.3) Collapse to yield sums of interest income and fixed claims by 	
			group, then clean up a bit
	***************************************************************************/

	foreach unweighted of varlist intinc inttaxw { // Weight variables to collapse
		qui replace `unweighted' = `unweighted' * wgt
	}

	collapse (sum) y = intinc a = inttaxw, by(year `rankvar'_grp)

	* Scale down into trillions
	qui replace y = y / 1E12
	qui replace a = a / 1E12 

	/***************************************************************************
		(2.4) Calculate interest rate
	***************************************************************************/

	qui gen r = (y / a) * 100 // Compute interest rate

	/***************************************************************************
		(2.5) Reshape data as wide and compute top group's share of fixed claims
	***************************************************************************/

	qui reshape wide y a r, i(year) j(`rankvar'_grp) // Reshape by group

	qui gen a_total_scf = a1 + a2
	qui gen sa1 = a1 / a_total // Compute top 1's share of fixed claims assets

	qui summ sa1 // Report top 1's average share of fixed claims assets over years
	qui egen mean_sa1 = mean(sa1) // Store avg in variables
	drop sa1

	/***************************************************************************
		(2.6) Merge on CPI adjustment factor and scale to 2019 dollars; also, 
			put variables of interest in logs
	***************************************************************************/

	qui merge 1:1 year using `fred', assert(2 3) keep(3) keepusing(cpi_adjfactor) nogen

	foreach dollarvar of varlist y1 y2 a1 a2 a_total_scf {
		qui replace `dollarvar' = `dollarvar' * cpi_adjfactor
	}
	drop cpi_adjfactor

	foreach logvar of varlist y1 y2 a1 a2 r1 r2 a_total_scf {
		gen ln_`logvar' = ln(`logvar')
	}

	/***************************************************************************
		(2.7) Clean up, sort, and save as tempfile
	***************************************************************************/

	isid year
	sort year

	tempfile scf`rankvar'
	qui save `scf`rankvar''
}

/*******************************************************************************
	(3) Prepare tax data
*******************************************************************************/

	/***************************************************************************
		(3.1) Rank by non-interest wealth (this is hweal112 - taxbond_3tier 
			before 2001, hweal112 - taxbond_info afterwards)
	***************************************************************************/

tempfile early mid

load_taxdata, rankspec($niw_defn_early) units("es") startyr(1966) endyr(1979)
qui save `early'

load_taxdata, rankspec($niw_defn_mid) units("es") startyr(1980) endyr(2000)
qui save `mid'

load_taxdata, rankspec($niw_defn_late) units("es") startyr(2001) endyr(2016)

qui append using `mid'
qui append using `early'

qui keep if inlist(group, "All", "P99-100")

qui replace group = cond(group == "All", "_all", "_top1")

qui gen fixinc = fiint + intest
drop fiint intest

keep fixinc taxbond_ini year group
rename taxbond_ini taxbond

qui reshape wide fixinc taxbond, i(year) j(group, string)

qui gen fixinc_bot99 = fixinc_all - fixinc_top1 

qui gen r_bar = (fixinc_all / taxbond_all) * 100

keep year fixinc_all fixinc_top1 fixinc_bot99 taxbond_all r_bar

rename (fixinc_all fixinc_top1 fixinc_bot99 taxbond_all) (y_total y1 y2 a_total)

qui merge 1:1 year using `fred', keepusing(cpi_adjfactor) assert(2 3) keep(3) nogen

foreach dollarvar of varlist y1 y2 y_total a_total {
	qui replace `dollarvar' = `dollarvar' / 1E12 // Scale down into trillions
	qui replace `dollarvar' = `dollarvar' * cpi_adjfactor
}
drop cpi_adjfactor

foreach logvar of varlist y1 y2 a_total r_bar {
	qui gen ln_`logvar' = ln(`logvar')
}

tempfile taxdata_niwranks
save `taxdata_niwranks'

	/***************************************************************************
		(3.2) Rank by income flows: fixinc (=fiint + intest), AGI, and AGI minus
			fixinc.
	***************************************************************************/

foreach rankvar in fixinc agi nonintagi {
	
	use $rawdir/.../fixinc_total_ranked_by_`rankvar'.dta, clear

	qui levelsof group, local(groups) clean
	assert "`groups'" == "P0-90 P90-99 P99-99.9 P99.9-99.99 P99.99-100"

	gen consolidated_grp = cond(inlist(group, "P0-90", "P90-99"), 2, 1)

	collapse (sum) y = fixinc, by(year consolidated_grp)

	reshape wide y, i(year) j(consolidated_grp)

	assert !missing(y1) & !missing(y2)

	qui merge 1:1 year using `fred', keepusing(cpi_adjfactor) assert(2 3) keep(3) nogen

	foreach dollarvar of varlist y1 y2 {
		qui replace `dollarvar' = `dollarvar' / 1E12 // Scale into trillions
		qui replace `dollarvar' = `dollarvar' * cpi_adjfactor
		qui gen ln_`dollarvar' = ln(`dollarvar')
	}
	drop cpi_adjfactor

	sort year

	tempfile taxdata_`rankvar'ranks
	save `taxdata_`rankvar'ranks'
}

/*******************************************************************************
	(4) Merge data together
*******************************************************************************/

	/***************************************************************************
		(4.1) Merge on collapses from tax data (incl DINA for the moment) and 
			rename flow concepts to keep track of what's ranked by what
	***************************************************************************/

use `taxdata_fixincranks', clear
rename *y? *y?_fixincrank

merge 1:1 year using `taxdata_agiranks', assert(3) nogen
rename *y? *y?_fiincrank

merge 1:1 year using `taxdata_niwranks', assert(3) nogen
rename *y? *y?_niwrank

merge 1:1 year using `taxdata_nonintagiranks', assert(3) nogen
rename *y? *y?_nonintagirank

	/***************************************************************************
		(4.2) Merge on parameters aggregates and compute and r bar with updated
			SZ 2020 aggregate fixed claims concepts
	***************************************************************************/

merge 1:1 year using `parameters', keepusing(a_total_sz20 ln_a_total_sz20) assert(2 3)
assert year > 2016 if _merge == 2
assert _merge == 3 if year < 2017
drop _merge

qui gen r_bar_sz20 = (y_total / a_total_sz20) * 100
qui gen ln_r_bar_sz20 = ln(r_bar_sz20)
drop y_total

	/***************************************************************************
		(4.3) Merge on credit and interest rate risk measures from FRED
	***************************************************************************/

merge 1:1 year using `fred', keepusing(r_* ln_r_*) assert(3) nogen

	/***************************************************************************
		(4.4) Merge on SCF collapses, renaming concepts so that we can keep 
			track of different ranks
	***************************************************************************/

foreach rankvar in niw intinc agi nonintagi { // Cycle through ranking variables

	merge 1:1 year using `scf`rankvar'', assert(1 3)
	assert _merge == 1 if !inrange(year, 1989, 2019) | mod(year - 1989, 3) > 0
	assert _merge == 3 if inrange(year, 1989, 2019) & mod(year - 1989, 3) == 0
	drop _merge

 	#delimit ;
	rename (*y? *a1 *a2 *r?) 
		(*y?_scf_`rankvar'rank *a1_scf_`rankvar'rank *a2_scf_`rankvar'rank *r?_scf_`rankvar'rank);
	#delimit cr
}

	/***************************************************************************
		(4.5) Clean up, sort, and save
	***************************************************************************/

qui ds year, not
order `r(varlist)', alphabetic last

isid year
sort year
export delimited using "~\cmd_replication_public\matlab\build\cmdinputs_top1bot99_20210615.csv", replace